//
//  CTPhotoBrowserIndicatorView.swift
//  CTPhotoBrowser
//
//  Created by Andney Jack on 2021/2/25.
//

import UIKit

/// 进度指示器
public class CTPhotoBrowserIndicatorView: UIView {
    /// 指示器样式
    public enum IndicatorStyle {
        case circle // 圆环形
        case pie // 饼形
    }

    private var m_progress: CGFloat = 0.0

    /// 指示器样式，默认为圆环形
    public var indicatorStyle: IndicatorStyle = .pie
    /// 指示圆环的颜色，默认为白色
    public var indicatorColor: UIColor = .blue
    /// 外围的边距
    public var outerMargin: CGFloat = 0.0
    /// 圆环线的宽度
    public var indicatorLineWidth: CGFloat = 4.0
    /// 设置圆环进度
    public var progress: CGFloat {
        get {
            return m_progress
        }
        set {
            m_progress = newValue
            if Thread.isMainThread {
                setNeedsDisplay()
            } else {
                DispatchQueue.main.async { [weak self] in
                    self?.setNeedsLayout()
                }
            }
        }
    }

    public required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override public init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 0, alpha: 0.3)
        layer.masksToBounds = true
        indicatorLineWidth = (indicatorStyle == .pie) ? 2.0 : 4.0
    }

    override public func layoutSubviews() {
        super.layoutSubviews()

        if bounds.width > 0, bounds.height > 0 {
            layer.cornerRadius = min(bounds.width, bounds.height) / 2.0
        }
    }

    override public func draw(_ rect: CGRect) {
        switch indicatorStyle {
        case .pie:
            m_drawPie(rect)
        default:
            m_drawCircle(rect)
        }
    }

    // 圆环形
    private func m_drawCircle(_ rect: CGRect) {
        let radius = min(rect.width, rect.height) / 2.0 - outerMargin - indicatorLineWidth / 2.0 + 0.2
        if radius <= 0 {
            return
        }

        let center = CGPoint(x: rect.width / 2.0, y: rect.height / 2.0)
        let from = -CGFloat(Double.pi / 2.0)
        let to = CGFloat(-Double.pi / 2.0 + Double(progress) * Double.pi * 2.0)

        let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: from, endAngle: to, clockwise: true)
        path.lineWidth = indicatorLineWidth
        path.lineCapStyle = .round

        indicatorColor.set()
        path.stroke()
    }

    // 饼形
    private func m_drawPie(_ rect: CGRect) {
        let radius = min(rect.width, rect.height) / 2.0
        if radius <= 0 {
            return
        }

        let circlePath = UIBezierPath(roundedRect: rect, cornerRadius: radius)
        circlePath.lineWidth = indicatorLineWidth

        indicatorColor.set()
        circlePath.stroke()

        let center = CGPoint(x: rect.width / 2.0, y: rect.height / 2.0)
        let end = CGFloat(-Double.pi / 2.0 + Double(progress) * Double.pi * 2.0)
        let path = UIBezierPath(arcCenter: center, radius: radius - indicatorLineWidth / 2.0, startAngle: CGFloat(-Double.pi / 2.0), endAngle: end, clockwise: true)
        path.addLine(to: center)
        path.close()

        indicatorColor.set()
        path.stroke()
        path.fill()
    }
}
